#include "single_list.h"

#include <stdlib.h>

void list_add_head(struct list *head,struct list *new)
{
    new->next = head->next;
    head->next = new;
}

void list_del_head(struct list *head)
{
    head->next = head->next->next;
}

void list_add_tail(struct list *head,struct list *new)
{
    struct list *pTmp = head;
    
    for(;pTmp->next;pTmp = pTmp->next);

    pTmp->next = new;
    new->next = NULL;
}

void list_del_tail(struct list *head)
{
    struct list *pTmp = head;
    struct list *prev = head;
    
    for(;pTmp->next;prev = pTmp,pTmp = pTmp->next);

    prev->next = NULL;
}

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

void list_for_each(struct list *head)
{
    for(struct list *pTmp = head->next;pTmp;pTmp = pTmp->next)
    {
        printf("%p\r\n",pTmp);
    }
}

struct list *list_new(void)
{
    return (struct list *)malloc(sizeof(struct list));
}

struct list head = {NULL};

int main(int argc,char *argv[])
{
    struct list *new = list_new();
    list_add_tail(&head,new);
    list_for_each(&head);
    printf("------------\r\n");

    struct list *new1 = list_new();
    list_add_head(&head,new1);
    list_for_each(&head);
    printf("------------\r\n");

    struct list *new2 = list_new();
    list_add_tail(&head,new2);
    list_for_each(&head);
    printf("------------\r\n");

    struct list *new3 = list_new();
    list_add_tail(&head,new3);
    list_for_each(&head);
    printf("------------\r\n");

    list_del_head(&head);
    list_for_each(&head);
    printf("------------\r\n");

    list_del_tail(&head);
    list_for_each(&head);
    printf("------------\r\n");

    list_del_tail(&head);
    list_for_each(&head);
    printf("------------\r\n");

    return 0;
}
